package xmu.zfw.dao;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import xmu.zfw.domain.Report;
import xmu.zfw.domain.ReportRelation;
import xmu.zfw.domain.Reporteder;
import xmu.zfw.domain.Reporter;
import xmu.zfw.mapper.ReportMapper;
import xmu.zfw.mapper.ReportRelationMapper;
import xmu.zfw.mapper.ReportederMapper;
import xmu.zfw.mapper.ReporterMapper;

import java.sql.Date;
import java.util.List;

@Component
public class ReportDao {

    @Autowired
    private ReportMapper reportMapper;

    @Autowired
    private ReporterMapper reporterMapper;

    @Autowired
    private ReportederMapper reportederMapper;

    @Autowired
    private ReportRelationMapper reportRelationMapper;

    public Report getReport(Report report){
        Reporter reporter = reporterMapper.getReporterById(report.getReporterId());
        report.setReporter(reporter);
        List<Reporteder> reporteders = reportederMapper.getReportedersByReportId(report.getId());
        report.setReporteders(reporteders);
        return report;
    }

    public List<Report> getReports(List<Report> reports){
        for(int i = 0; i < reports.size(); i++){
            reports.set(i, getReport(reports.get(i)));
        }
        return reports;
    }

    public Report getReportById(Integer id){
        Report report = reportMapper.getReportById(id);
        if(report == null){
            return null;
        }
        report = getReport(report);
        return  report;
    }

    public Report getReportByNo(String no){
        Report report = reportMapper.getReportByNo(no);
        if(report == null){
            return null;
        }
        report = getReport(report);
        return  report;
    }

    public int addReport(Report report){
        Reporter reporter = report.getReporter();
        reporterMapper.addReporter(reporter);
        report.setReporterId(reporter.getId());
        int line = reportMapper.addReport(report);
        updateReportRelation(report.getId(), report.getReporteders());

        return line;
    }

    public Report updateReport(Report report){
        Report tmpReport = reportMapper.getReportById(report.getId());
        if(tmpReport == null){
            return null;
        }

//        report.setReporterId(tmpReport.getReporterId());
        if(report.getReporter() != null){
            Reporter reporter = report.getReporter();
            reporter.setId(tmpReport.getReporterId());
            reporterMapper.updateReporter(reporter);
        }

        updateReportRelation(report.getId(), report.getReporteders());
        reportMapper.modifyReport(report);

        return report;
    }

    public List<Report> searchReports(Report report, Date startTime, Date endTime){
        List<Report> reports = reportMapper.searchReport(report, startTime, endTime);
        for(int i = 0; i < reports.size(); i++){
            Report tmpReport = reports.get(i);
            tmpReport.setReporter(reporterMapper.getReporterById(tmpReport.getReporterId()));
            tmpReport.setReporteders(reportederMapper.getReportedersByReportId(tmpReport.getId()));
            reports.set(i, tmpReport);
        }
        return reports;
    }

    public List<Report> searchManyReportedReports(Report report, Date startTime, Date endTime){
        List<Report> reports = reportMapper.searchManyReportedReports(report, startTime, endTime);
        for(int i = 0; i < reports.size(); i++){
            Report tmpReport = reports.get(i);
            tmpReport.setReporter(reporterMapper.getReporterById(tmpReport.getReporterId()));
            tmpReport.setReporteders(reportederMapper.getReportedersByReportId(tmpReport.getId()));
            reports.set(i, tmpReport);
        }
        return reports;
    }



    public Integer deleteReport(Integer id){
        reportRelationMapper.deleteReportRelationsByReportId(id);
        return reportMapper.deleteReportById(id);
    }

    private void updateReportRelation(Integer reportId, List<Reporteder> reporteders){
        reportRelationMapper.deleteReportRelationsByReportId(reportId);

        for(Reporteder reporteder: reporteders){
            Reporteder tmpReporteder = reportederMapper.getReporteder(reporteder);
            if(tmpReporteder == null){
                reportederMapper.addReporteder(reporteder);
            }else{
                reporteder.setId(tmpReporteder.getId());
            }
            ReportRelation reportRelation = new ReportRelation();
            reportRelation.setReportId(reportId);
            reportRelation.setReportederId(reporteder.getId());
            reportRelationMapper.addReportRelation(reportRelation);
        }
    }
}
